<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html id="htmlId">
<head>
    <title>Coverage Report :: Launcher</title>
    <style type="text/css">
        @import "../../.css/coverage.css";
    </style>
</head>

<body>
<div class="header"></div>

<div class="content">
    <div class="breadCrumbs">
        [ <a href="../../index.html">all classes</a> ]
        [ <a href="../index.html">nl.tudelft.jpacman</a> ]
    </div>

    <h1>Coverage Summary for Class: Launcher (nl.tudelft.jpacman)</h1>

    <table class="coverageStats">
        <tr>
            <th class="name">Class</th>
            <th class="coverageStat
">
                Class, %
            </th>
            <th class="coverageStat
">
                Method, %
            </th>
            <th class="coverageStat
">
                Line, %
            </th>
        </tr>
        <tr>
            <td class="name">Launcher</td>
            <td class="coverageStat">
  <span class="percent">
    0%
  </span>
                <span class="absValue">
    (0/ 1)
  </span>
            </td>
            <td class="coverageStat">
  <span class="percent">
    0%
  </span>
                <span class="absValue">
    (0/ 22)
  </span>
            </td>
            <td class="coverageStat">
  <span class="percent">
    0%
  </span>
                <span class="absValue">
    (0/ 43)
  </span>
            </td>
        </tr>

    </table>

    <br/>
    <br/>


    <div class="sourceCode"><i>1</i>&nbsp;package nl.tudelft.jpacman;
        <i>2</i>&nbsp;
        <i>3</i>&nbsp;import java.awt.event.KeyEvent;
        <i>4</i>&nbsp;import java.io.IOException;
        <i>5</i>&nbsp;import java.util.List;
        <i>6</i>&nbsp;
        <i>7</i>&nbsp;import nl.tudelft.jpacman.board.BoardFactory;
        <i>8</i>&nbsp;import nl.tudelft.jpacman.board.Direction;
        <i>9</i>&nbsp;import nl.tudelft.jpacman.game.Game;
        <i>10</i>&nbsp;import nl.tudelft.jpacman.game.GameFactory;
        <i>11</i>&nbsp;import nl.tudelft.jpacman.level.Level;
        <i>12</i>&nbsp;import nl.tudelft.jpacman.level.LevelFactory;
        <i>13</i>&nbsp;import nl.tudelft.jpacman.level.MapParser;
        <i>14</i>&nbsp;import nl.tudelft.jpacman.level.Player;
        <i>15</i>&nbsp;import nl.tudelft.jpacman.level.PlayerFactory;
        <i>16</i>&nbsp;import nl.tudelft.jpacman.npc.ghost.GhostFactory;
        <i>17</i>&nbsp;import nl.tudelft.jpacman.points.PointCalculator;
        <i>18</i>&nbsp;import nl.tudelft.jpacman.points.PointCalculatorLoader;
        <i>19</i>&nbsp;import nl.tudelft.jpacman.sprite.PacManSprites;
        <i>20</i>&nbsp;import nl.tudelft.jpacman.ui.Action;
        <i>21</i>&nbsp;import nl.tudelft.jpacman.ui.PacManUI;
        <i>22</i>&nbsp;import nl.tudelft.jpacman.ui.PacManUiBuilder;
        <i>23</i>&nbsp;
        <i>24</i>&nbsp;/**
        <i>25</i>&nbsp; * Creates and launches the JPacMan UI.
        <i>26</i>&nbsp; *
        <i>27</i>&nbsp; * @author Jeroen Roosen
        <i>28</i>&nbsp; */
        <b class="nc"><i>29</i>&nbsp;@SuppressWarnings(&quot;PMD.TooManyMethods&quot;)</b>
        <b class="nc"><i>30</i>&nbsp;public class Launcher {</b>
        <i>31</i>&nbsp;
        <b class="nc"><i>32</i>&nbsp; private static final PacManSprites SPRITE_STORE = new PacManSprites();</b>
        <i>33</i>&nbsp;
        <i>34</i>&nbsp; public static final String DEFAULT_MAP = &quot;/board.txt&quot;;
        <b class="nc"><i>35</i>&nbsp; private String levelMap = DEFAULT_MAP;</b>
        <i>36</i>&nbsp;
        <i>37</i>&nbsp; private PacManUI pacManUI;
        <i>38</i>&nbsp; private Game game;
        <i>39</i>&nbsp;
        <i>40</i>&nbsp; /**
        <i>41</i>&nbsp; * @return The game object this launcher will start when {@link #launch()}
        <i>42</i>&nbsp; * is called.
        <i>43</i>&nbsp; */
        <i>44</i>&nbsp; public Game getGame() {
        <b class="nc"><i>45</i>&nbsp; return game;</b>
        <i>46</i>&nbsp; }
        <i>47</i>&nbsp;
        <i>48</i>&nbsp; /**
        <i>49</i>&nbsp; * The map file used to populate the level.
        <i>50</i>&nbsp; *
        <i>51</i>&nbsp; * @return The name of the map file.
        <i>52</i>&nbsp; */
        <i>53</i>&nbsp; protected String getLevelMap() {
        <b class="nc"><i>54</i>&nbsp; return levelMap;</b>
        <i>55</i>&nbsp; }
        <i>56</i>&nbsp;
        <i>57</i>&nbsp; /**
        <i>58</i>&nbsp; * Set the name of the file containing this level&#39;s map.
        <i>59</i>&nbsp; *
        <i>60</i>&nbsp; * @param fileName Map to be used.
        <i>61</i>&nbsp; * @return Level corresponding to the given map.
        <i>62</i>&nbsp; */
        <i>63</i>&nbsp; public Launcher withMapFile(String fileName) {
        <b class="nc"><i>64</i>&nbsp; levelMap = fileName;</b>
        <b class="nc"><i>65</i>&nbsp; return this;</b>
        <i>66</i>&nbsp; }
        <i>67</i>&nbsp;
        <i>68</i>&nbsp; /**
        <i>69</i>&nbsp; * Creates a new game using the level from {@link #makeLevel()}.
        <i>70</i>&nbsp; *
        <i>71</i>&nbsp; * @return a new Game.
        <i>72</i>&nbsp; */
        <i>73</i>&nbsp; public Game makeGame() {
        <b class="nc"><i>74</i>&nbsp; GameFactory gf = getGameFactory();</b>
        <b class="nc"><i>75</i>&nbsp; Level level = makeLevel();</b>
        <b class="nc"><i>76</i>&nbsp; game = gf.createSinglePlayerGame(level, loadPointCalculator());</b>
        <b class="nc"><i>77</i>&nbsp; return game;</b>
        <i>78</i>&nbsp; }
        <i>79</i>&nbsp;
        <i>80</i>&nbsp; private PointCalculator loadPointCalculator() {
        <b class="nc"><i>81</i>&nbsp; return new PointCalculatorLoader().load();</b>
        <i>82</i>&nbsp; }
        <i>83</i>&nbsp;
        <i>84</i>&nbsp; /**
        <i>85</i>&nbsp; * Creates a new level. By default this method will use the map parser to
        <i>86</i>&nbsp; * parse the default board stored in the &lt;code&gt;board.txt&lt;/code&gt; resource.
        <i>87</i>&nbsp; *
        <i>88</i>&nbsp; * @return A new level.
        <i>89</i>&nbsp; */
        <i>90</i>&nbsp; public Level makeLevel() {
        <i>91</i>&nbsp; try {
        <b class="nc"><i>92</i>&nbsp; return getMapParser().parseMap(getLevelMap());</b>
        <b class="nc"><i>93</i>&nbsp; } catch (IOException e) {</b>
        <b class="nc"><i>94</i>&nbsp; throw new PacmanConfigurationException(</b>
        <b class="nc"><i>95</i>&nbsp; &quot;Unable to create level, name = &quot; + getLevelMap(), e);</b>
        <i>96</i>&nbsp; }
        <i>97</i>&nbsp; }
        <i>98</i>&nbsp;
        <i>99</i>&nbsp; /**
        <i>100</i>&nbsp; * @return A new map parser object using the factories from
        <i>101</i>&nbsp; * {@link #getLevelFactory()} and {@link #getBoardFactory()}.
        <i>102</i>&nbsp; */
        <i>103</i>&nbsp; protected MapParser getMapParser() {
        <b class="nc"><i>104</i>&nbsp; return new MapParser(getLevelFactory(), getBoardFactory());</b>
        <i>105</i>&nbsp; }
        <i>106</i>&nbsp;
        <i>107</i>&nbsp; /**
        <i>108</i>&nbsp; * @return A new board factory using the sprite store from
        <i>109</i>&nbsp; * {@link #getSpriteStore()}.
        <i>110</i>&nbsp; */
        <i>111</i>&nbsp; protected BoardFactory getBoardFactory() {
        <b class="nc"><i>112</i>&nbsp; return new BoardFactory(getSpriteStore());</b>
        <i>113</i>&nbsp; }
        <i>114</i>&nbsp;
        <i>115</i>&nbsp; /**
        <i>116</i>&nbsp; * @return The default {@link PacManSprites}.
        <i>117</i>&nbsp; */
        <i>118</i>&nbsp; protected PacManSprites getSpriteStore() {
        <b class="nc"><i>119</i>&nbsp; return SPRITE_STORE;</b>
        <i>120</i>&nbsp; }
        <i>121</i>&nbsp;
        <i>122</i>&nbsp; /**
        <i>123</i>&nbsp; * @return A new factory using the sprites from {@link #getSpriteStore()}
        <i>124</i>&nbsp; * and the ghosts from {@link #getGhostFactory()}.
        <i>125</i>&nbsp; */
        <i>126</i>&nbsp; protected LevelFactory getLevelFactory() {
        <b class="nc"><i>127</i>&nbsp; return new LevelFactory(getSpriteStore(), getGhostFactory(),
            loadPointCalculator());</b>
        <i>128</i>&nbsp; }
        <i>129</i>&nbsp;
        <i>130</i>&nbsp; /**
        <i>131</i>&nbsp; * @return A new factory using the sprites from {@link #getSpriteStore()}.
        <i>132</i>&nbsp; */
        <i>133</i>&nbsp; protected GhostFactory getGhostFactory() {
        <b class="nc"><i>134</i>&nbsp; return new GhostFactory(getSpriteStore());</b>
        <i>135</i>&nbsp; }
        <i>136</i>&nbsp;
        <i>137</i>&nbsp; /**
        <i>138</i>&nbsp; * @return A new factory using the players from {@link #getPlayerFactory()}.
        <i>139</i>&nbsp; */
        <i>140</i>&nbsp; protected GameFactory getGameFactory() {
        <b class="nc"><i>141</i>&nbsp; return new GameFactory(getPlayerFactory());</b>
        <i>142</i>&nbsp; }
        <i>143</i>&nbsp;
        <i>144</i>&nbsp; /**
        <i>145</i>&nbsp; * @return A new factory using the sprites from {@link #getSpriteStore()}.
        <i>146</i>&nbsp; */
        <i>147</i>&nbsp; protected PlayerFactory getPlayerFactory() {
        <b class="nc"><i>148</i>&nbsp; return new PlayerFactory(getSpriteStore());</b>
        <i>149</i>&nbsp; }
        <i>150</i>&nbsp;
        <i>151</i>&nbsp; /**
        <i>152</i>&nbsp; * Adds key events UP, DOWN, LEFT and RIGHT to a game.
        <i>153</i>&nbsp; *
        <i>154</i>&nbsp; * @param builder The {@link PacManUiBuilder} that will provide the UI.
        <i>155</i>&nbsp; */
        <i>156</i>&nbsp; protected void addSinglePlayerKeys(final PacManUiBuilder builder) {
        <b class="nc"><i>157</i>&nbsp; builder.addKey(KeyEvent.VK_UP, moveTowardsDirection(Direction.NORTH))</b>
        <b class="nc"><i>158</i>&nbsp; .addKey(KeyEvent.VK_DOWN, moveTowardsDirection(Direction.SOUTH))</b>
        <b class="nc"><i>159</i>&nbsp; .addKey(KeyEvent.VK_LEFT, moveTowardsDirection(Direction.WEST))</b>
        <b class="nc"><i>160</i>&nbsp; .addKey(KeyEvent.VK_RIGHT, moveTowardsDirection(Direction.EAST));</b>
        <i>161</i>&nbsp; }
        <i>162</i>&nbsp;
        <i>163</i>&nbsp; private Action moveTowardsDirection(Direction direction) {
        <b class="nc"><i>164</i>&nbsp; return () -&gt; {</b>
        <b class="nc"><i>165</i>&nbsp; assert game != null;</b>
        <b class="nc"><i>166</i>&nbsp; getGame().move(getSinglePlayer(getGame()), direction);</b>
        <i>167</i>&nbsp; };
        <i>168</i>&nbsp; }
        <i>169</i>&nbsp;
        <i>170</i>&nbsp; private Player getSinglePlayer(final Game game) {
        <b class="nc"><i>171</i>&nbsp; List&lt;Player&gt; players = game.getPlayers();</b>
        <b class="nc"><i>172</i>&nbsp; if (players.isEmpty()) {</b>
        <b class="nc"><i>173</i>&nbsp; throw new IllegalArgumentException(&quot;Game has 0 players.&quot;);</b>
        <i>174</i>&nbsp; }
        <b class="nc"><i>175</i>&nbsp; return players.get(0);</b>
        <i>176</i>&nbsp; }
        <i>177</i>&nbsp;
        <i>178</i>&nbsp; /**
        <i>179</i>&nbsp; * Creates and starts a JPac-Man game.
        <i>180</i>&nbsp; */
        <i>181</i>&nbsp; public void launch() {
        <b class="nc"><i>182</i>&nbsp; makeGame();</b>
        <b class="nc"><i>183</i>&nbsp; PacManUiBuilder builder = new PacManUiBuilder().withDefaultButtons();</b>
        <b class="nc"><i>184</i>&nbsp; addSinglePlayerKeys(builder);</b>
        <b class="nc"><i>185</i>&nbsp; pacManUI = builder.build(getGame());</b>
        <b class="nc"><i>186</i>&nbsp; pacManUI.start();</b>
        <i>187</i>&nbsp; }
        <i>188</i>&nbsp;
        <i>189</i>&nbsp; /**
        <i>190</i>&nbsp; * Disposes of the UI. For more information see
        <i>191</i>&nbsp; * {@link javax.swing.JFrame#dispose()}.
        <i>192</i>&nbsp; * &lt;p&gt;
        <i>193</i>&nbsp; * Precondition: The game was launched first.
        <i>194</i>&nbsp; */
        <i>195</i>&nbsp; public void dispose() {
        <b class="nc"><i>196</i>&nbsp; assert pacManUI != null;</b>
        <b class="nc"><i>197</i>&nbsp; pacManUI.dispose();</b>
        <i>198</i>&nbsp; }
        <i>199</i>&nbsp;
        <i>200</i>&nbsp; /**
        <i>201</i>&nbsp; * Main execution method for the Launcher.
        <i>202</i>&nbsp; *
        <i>203</i>&nbsp; * @param args The command line arguments - which are ignored.
        <i>204</i>&nbsp; * @throws IOException When a resource could not be read.
        <i>205</i>&nbsp; */
        <i>206</i>&nbsp; public static void main(String[] args) throws IOException {
        <b class="nc"><i>207</i>&nbsp; new Launcher().launch();</b>
        <i>208</i>&nbsp; }
        <i>209</i>&nbsp;}
    </div>
</div>

<div class="footer">

    <div style="float:right;">generated on 2023-04-25 16:45</div>
</div>
</body>
</html>
